﻿using BCGM.MedicalWisdom.Model;
using Microsoft.AspNetCore.Mvc;
using BCGM.MedicalWisdom.IServices.Orders;
using System.Linq.Expressions;
using BCGM.MedicalWisdom.Model.Models.Order;

namespace BCGM.MedicalWisdom.Api.Controllers.Orders
{
    /// <summary>
    /// 后台-审核管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuditController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IAuditServices _auditServices;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="auditServices"></param>
        public AuditController(IAuditServices auditServices)
        {
            _auditServices = auditServices;
        }

        /// <summary>
        /// 查询显示+分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="key"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]//请求方法
        //Task<T>异步任务 T表示改任务的返回数据类型 
        //MessageModel返回通用信息类
        //PageModel通用分页信息类
        public async Task<MessageModel<PageModel<Audit>>> Get(int pageIndex = 1, string  key = "医院", int pageSize = 5)
        {
            int physician = 0;
            int agelimit = 0;

            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "医院";
            }
            //一个条件  查询条件没出来是因为需要先判断 string.IsNullOrEmpty(key) == false 
            Expression<Func<Audit, bool>> whereExpression = d => (d.IsDeleted == false) && (string.IsNullOrEmpty(key) == false && d.Hospital.Contains(key)) || (string.IsNullOrEmpty(key) == false && d.Physician == physician) || (string.IsNullOrEmpty(key) == false && d.AgeLimit == agelimit);

            var audit = await _auditServices.QueryPage(whereExpression, pageIndex, pageSize);
             
            //返回作用信息类
            return new MessageModel<PageModel<Audit>>()
            {
                msg = "获取成功",
                success = true,
                response = audit,
                //1.为什么要用异步

            };

        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<Audit>> Get(string id)
        {
            return new MessageModel<Audit>()
            {
                msg = "获取成功",
                success = true,
                response = await _auditServices.QueryById(id)
            };
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] Audit request)
        {
            var data = new MessageModel<string>();

            var id = await _auditServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] Audit request)
        {
            var data = new MessageModel<string>();
            data.success = await _auditServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.AuditName.ObjToString();
            }

            return data;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            var model = await _auditServices.QueryById(id);
            model.IsDeleted = true;
            data.success = await _auditServices.Delete(model);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = model?.AuditName.ObjToString();
            }

            return data;
        }
        ///// <summary>
        ///// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        ///// </summary>
        //private readonly IDoctorServices _doctorServices;
        ///// <summary>
        ///// 构造函数
        ///// </summary>
        ///// <param name="doctorServices"></param>
        //public DoctorController(IDoctorServices doctorServices)
        //{
        //    _doctorServices = doctorServices;
        //}

        ///// <summary>
        ///// 医生列表
        ///// </summary>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <param name="hospitalname"></param>
        ///// <param name="agelimit"></param>
        ///// <param name="status"></param>
        ///// <returns></returns>
        //[HttpGet]
        ////异步接口可以提高并发量、吞吐量
        //public async Task<MessageModel> DoctorList(int pageIndex = 1, int pageSize = 100, string hospitalname = "", int agelimit = 0, int status = 1)
        //{
        //    var data = await _doctorServices.DoctorList(pageIndex, pageSize, hospitalname, agelimit, status);
        //    return new MessageModel
        //    {
        //        msg = "数据获取成功",
        //        status = 200,
        //        success = true,
        //        response = data.page,
        //        count = data.count
        //    };
        //}
        ///// <summary>
        ///// 医生信息详情
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<MessageModel> GetDoctor(int id)
        //{
        //    var data = await _doctorServices.GetDoctor(id);
        //    return new MessageModel
        //    {
        //        msg = "数据获取成功",
        //        status = 200,
        //        success = true,
        //        response = data
        //    };
        //}
        ///// <summary>
        ///// 医生审核
        ///// </summary>
        ///// <param name="a"></param>
        ///// <returns></returns>
        //[HttpPut]
        //public async Task<MessageModel> DoctorUpt([FromBody] DoctorUptDto a)
        //{
        //    var data = await _doctorServices.DoctorUpt(a);
        //    if (data == true)
        //    {
        //        return new MessageModel
        //        {
        //            msg = "数据修改成功",
        //            status = 200,
        //            success = true,
        //            response = data
        //        };
        //    }
        //    else
        //    {
        //        return new MessageModel
        //        {
        //            msg = "数据获取失败",
        //            status = 500,
        //            success = false,
        //            response = data
        //        };
        //    }

        // var data = new MessageModel<string>();
        //data.success = await _doctorServices.Update(a);
        //if (data.success)
        //{
        //    data.msg = "更新成功";
        //    data.response = a?.Id.ObjToString();
        //}
        //else
        //{
        //    data.msg = "更新失败";
        //    data.response = a?.Id.ObjToString();
        //}
        //return data;
        //}
        //[HttpGet]
        //[Route("Doctor")]
        //public string Get()
        //{
        //    return "fwrv";
        //}
    }
}
